/* eslint-disable camelcase */
import React, { createContext, useContext, useMemo } from 'react';
import { DataSet } from 'choerodon-ui/pro';
import { get } from '@choerodon/inject';
import hostDockerConfigDataSet
  from '@/routes/app-center-pro/components/OpenAppCreateModal/components/host-docker-config/stores/hostDockerConfigDataSet';

interface ContextType {
  children: any;
  cRef: any;
  HostDockerConfigDataSet: any,
  detail?: any,
  refresh?: any,
  modal?: any,
  isDetail?: any,
  appInfo_appName?:string
  appInfo_appCode?:string
}

const Store = createContext({} as ContextType);

export function useDockerConfigStore() {
  return useContext(Store);
}

export const StoreProvider = (props: any) => {
  const {
    children,
    detail,
    appInfo_appName,
    appInfo_hostId,
  } = props;

  const isDetail = useMemo(() => Boolean(detail), [detail]);
  const hostGroupDeploy = get('configuration.devops-routes-pipeline-manage:hostGroupDeploy');
  const appCenterApi = get('configuration-devops-api-app-center-appCenterApi');

  const HostDockerConfigDataSet = useMemo(() => new DataSet(
    hostDockerConfigDataSet(isDetail, detail, hostGroupDeploy, appCenterApi,
      appInfo_appName, appInfo_hostId),
  ), [isDetail]);

  const value = {
    ...props,
    HostDockerConfigDataSet,
    isDetail,
  };

  return <Store.Provider value={value}>{children}</Store.Provider>;
};
